package com.itbour.web.action.trade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.KeywordInfoDto;
import com.itbour.dto.trade.ItemInfoDto;
import com.itbour.dto.trade.PrintParamInfoDto;
import com.itbour.service.trade.ItemsService;
import com.opensymphony.xwork2.ActionContext;

import freemarker.template.utility.StringUtil;

/**
 * copyright Itbour team 2016
 * 
 * @author xc
 */

public class GetItemsWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ItemsService itemsService;
	
	// [商品]
	private HashMap<String, Object> itemsMapResult = new HashMap<String, Object>();//[分页]商品信息列表
	private List<ItemInfoDto> itemsInfoList;// 商品信息列表
	private ItemInfoDto itemInfo;// 更新用
	private String itemId;// 商品id
	//private List<FeedbackFromInfoDto> fbFromComboList;// 反馈来源

	private String findPubFlg;// 检索条件[发布状态]
	private String findItemId;// 检索条件[商品ID]
	private String findItemNo;// 检索条件[商品编号]
	private String findItemName;// 检索条件[商品名称]
	private String findItemTypeL;// 检索条件[商品大分类]
	private String findItemTypeM;// 检索条件[商品中分类]
	private String findItemType;// 检索条件[商品分类]
	private String findItemState;// 检索条件[商品状态]
	private String findDesignId;// 检索条件[关联模版id]
	private String ids;// 通用ids[逗号连接]
	private String order;// 显示顺序
	
	private List<PrintParamInfoDto> ppInfoList;// 印刷参数列表
	private String relPpIds;//
	private String findComIndx;// 下标查询
	
	
	// 分页获取商品信息
	public String getItemsInfoPg() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		
		Map<String, Object> param = new HashMap<String, Object>();
		
		if (StringUtils.isNotEmpty(findItemId)) {
			param.put("item_id", findItemId);
		}
		if (StringUtils.isNotEmpty(findItemNo)) {
			param.put("item_no", "%"+findItemNo+"%");
		}
		if (StringUtils.isNotEmpty(findItemName)) {
			param.put("item_name", "%"+findItemName+"%");
		}
		if (StringUtils.isNotEmpty(findItemTypeL)) {
			param.put("type_id_l", findItemTypeL);
		}
		if (StringUtils.isNotEmpty(findItemTypeM)) {
			param.put("type_id_m", findItemTypeM);
		}
		if (StringUtils.isNotEmpty(findItemType)) {
			param.put("type_id", findItemType);
		}
		if (StringUtils.isNotEmpty(findItemState)) {
			param.put("state", findItemState);
		}
		if (StringUtils.isNotEmpty(findDesignId)) {
			param.put("design_id", findDesignId);
		}
		if (StringUtils.isNotEmpty(findComIndx)) {
			param.put("comImgIdx", findComIndx);
		}
//		if (StringUtils.isNotBlank(findPubFlg)) {
//			param.put("publish", findPubFlg);
//		}
		Page page = itemsService.itemsPgQuery(param,
				Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		itemsMapResult = resMap;
		return SUCCESS;
	}
	
	// 获取商品信息列表
	public String getItemsInfoLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("item_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("item_id", id);
		}
		itemsInfoList = itemsService.selectItemsInfo(param);
		return SUCCESS;
	}

	// 删除商品信息
	public void delItem() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user != null) {
    		itemsService.delItem(ids, user.getUser_id().toString());
    		this.writeJSON("ok");
    	} else {
    		this.writeJSON("ng");
    	}
	}
	
	// 添加商品信息
	public void addItem() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
		if (itemInfo != null && user != null) {
			itemInfo.setUpdate_by(user.getUser_id().toString());
			
	    	if(itemInfo.getItem_id()!=null && itemInfo.getItem_id()>0){
	    		// 更新
	    		this.updItem();
	    	} else {
	    		// 新建
	    		itemInfo.setState(10);// 编辑中
	    		itemsService.addItem(itemInfo);
	    		this.writeJSON("ok");
	    	}
		} else {
			this.writeJSON("ng");
		}
	}
	
	// 更新商品信息
	public void updItem() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		if (user != null && itemInfo != null && 
			((itemInfo.getItem_id() != null && itemInfo.getItem_id()>0) || StringUtils.isNotBlank(itemInfo.getIds()))) {
			
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("update_by", user.getUser_id());
	    	
	    	if(itemInfo.getItem_id() != null && itemInfo.getItem_id()>0){
		    	param.put("item_id", itemInfo.getItem_id());
	    	}
	    	if(StringUtils.isNotBlank(itemInfo.getIds())){
	    		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(itemInfo.getIds())));
	    	}
	    	if(itemInfo.getType_id() != null && itemInfo.getType_id()>0) {
	    		param.put("type_id", itemInfo.getType_id());
	    	}
	    	if(itemInfo.getState() != null && itemInfo.getState()>0) {
	    		param.put("state", itemInfo.getState());
	    	}
	    	if(itemInfo.getItem_no() != null) {
	    		param.put("item_no", itemInfo.getItem_no());
	    	}
	    	if(itemInfo.getItem_name() != null) {
	    		param.put("item_name", itemInfo.getItem_name());
	    	}
	    	if(itemInfo.getDesc() != null) {
	    		param.put("desc", itemInfo.getDesc());
	    	}
	    	if(itemInfo.getTheme_thumb() != null) {
	    		param.put("theme_thumb", itemInfo.getTheme_thumb());
	    	}
	    	if(itemInfo.getThumbs() != null) {
	    		param.put("thumbs", itemInfo.getThumbs());
	    	}
	    	if(itemInfo.getDesign_id() != null && itemInfo.getDesign_id()>0) {
	    		param.put("design_id", itemInfo.getDesign_id());
	    	}
	    	if(itemInfo.getComImgIdx()!=null){
	    		param.put("comImgIdx", itemInfo.getComImgIdx());
	    	}
	    	itemsService.updItem(param);
	    	this.writeJSON("ok");
	    	
		} else {
			this.writeJSON("ng");
		}
	}

	// 获取打印参数信息
	public String getPpInfoLst(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("itemId");
		String ppType = request.getParameter("ppType");
		
		Map<String, Object> param = new HashMap<String, Object>();
		if(id!=null && Long.valueOf(id)>0){
			param.put("item_id", id);
		}
		if(ppType!=null && Long.valueOf(ppType)>0){
			param.put("type", ppType);
		}
		ppInfoList = itemsService.selectPpInfo(param);
		if(ppInfoList != null && ppInfoList.size()>0){
			// TODO
		}

		return SUCCESS;
	}
	
	// 更新关联印刷参数
	public void updPpRelInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		if(user != null && itemId!=null && Long.valueOf(itemId)>0 && relPpIds!=null){
	    	// 删除旧信息
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("update_by", user.getUser_id());
	    	param.put("item_id", itemId);
	    	itemsService.delRelPpInfo(param);
	    	
	    	List<Long> ppIds = StringUtils.stringToList2(relPpIds);
	    	if(ppIds!=null && ppIds.size()>0) {
	    		param.put("state", 0);
	    		for(int i=0; i<ppIds.size(); i++) {
	    			// 更新信息
		    		param.put("pp_id", ppIds.get(i));
		    		itemsService.addRelPpInfo(param);
	    		}
	    	}
	    	this.writeJSON("ok");
		
		} else {
			this.writeJSON("ng");
		}
	}
	
	// 清除关联模版信息
	public void clrRelDesign() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		if(user != null && itemId!=null && Long.valueOf(itemId)>0){
	    	// 删除旧信息
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("update_by", user.getUser_id());
	    	param.put("item_id", itemId);
	    	itemsService.clrRelDesign(param);
	    	this.writeJSON("ok");
		} else {
			this.writeJSON("ng");
		}
	}
	
	public ItemsService getItemsService() {
		return itemsService;
	}

	public void setItemsService(ItemsService itemsService) {
		this.itemsService = itemsService;
	}

	public HashMap<String, Object> getItemsMapResult() {
		return itemsMapResult;
	}

	public void setItemsMapResult(HashMap<String, Object> itemsMapResult) {
		this.itemsMapResult = itemsMapResult;
	}

	public List<ItemInfoDto> getItemsInfoList() {
		return itemsInfoList;
	}

	public void setItemsInfoList(List<ItemInfoDto> itemsInfoList) {
		this.itemsInfoList = itemsInfoList;
	}

	public ItemInfoDto getItemInfo() {
		return itemInfo;
	}

	public void setItemInfo(ItemInfoDto itemInfo) {
		this.itemInfo = itemInfo;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public String getFindItemNo() {
		return findItemNo;
	}

	public void setFindItemNo(String findItemNo) {
		this.findItemNo = findItemNo;
	}

	public String getFindItemName() {
		return findItemName;
	}

	public void setFindItemName(String findItemName) {
		this.findItemName = findItemName;
	}

	public String getFindItemTypeL() {
		return findItemTypeL;
	}

	public void setFindItemTypeL(String findItemTypeL) {
		this.findItemTypeL = findItemTypeL;
	}

	public String getFindItemTypeM() {
		return findItemTypeM;
	}

	public void setFindItemTypeM(String findItemTypeM) {
		this.findItemTypeM = findItemTypeM;
	}

	public String getFindItemType() {
		return findItemType;
	}

	public void setFindItemType(String findItemType) {
		this.findItemType = findItemType;
	}

	public String getFindItemState() {
		return findItemState;
	}

	public void setFindItemState(String findItemState) {
		this.findItemState = findItemState;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<PrintParamInfoDto> getPpInfoList() {
		return ppInfoList;
	}

	public void setPpInfoList(List<PrintParamInfoDto> ppInfoList) {
		this.ppInfoList = ppInfoList;
	}

	public String getRelPpIds() {
		return relPpIds;
	}

	public void setRelPpIds(String relPpIds) {
		this.relPpIds = relPpIds;
	}

	public String getFindDesignId() {
		return findDesignId;
	}

	public void setFindDesignId(String findDesignId) {
		this.findDesignId = findDesignId;
	}

	public String getFindItemId() {
		return findItemId;
	}

	public void setFindItemId(String findItemId) {
		this.findItemId = findItemId;
	}

	public String getFindComIndx() {
		return findComIndx;
	}
	public void setFindComIndx(String findComIndx) {
		this.findComIndx = findComIndx;
	}
}
